<template>
  <div class='sub-category'>
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选分区 -->
       <SubFilter @filter-change="changeFilter" />
      <!-- 结果区域 -->
      <div class="goods-list">
         <!-- 排序 -->
         <SubSort @sort-change="changeSort" />
         <!-- 列表 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id" >
            <GoodsItem :goods="goods" />
          </li>
        </ul>
        <!-- 无限加载组件 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData"  />
      </div>
      <!-- 商品分区 -->
    </div>
  </div>
</template>

<script>
import {ref, watch} from 'vue'
import SubBread from './components/sub-bread.vue'
import SubFilter from './components/sub-filter'
import SubSort from './components/sub-sort'
import GoodsItem from './components/goods-item'
import { findSubCategoryGoods } from '@/api/category'
import { useRoute } from 'vue-router'

export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter,SubSort,GoodsItem},
  setup(){
    const route=useRoute()
    // 加载中
    const loading=ref(false);
    // 是否完成加载
    const finished=ref(false);
    // 商品列表数据
    const goodsList=ref([])
    // 请求参数
    let reqParams={
      page:1,
      pagesize:20,
    }
    const getData=()=>{
      loading.value=true;
      // 设置二级分类id
      reqParams.categoryId=route.params.id;
      findSubCategoryGoods(reqParams).then(({result})=>{
        // 获取数据成功
        if(result.items.length){
          // 有数据
          goodsList.value.push(...result.items)
          //把page改成 下一页页码
          reqParams.page++;
          // loading.value=false
        }else{
          // 没有数据，加载完成
          finished.value=true
        }
        loading.value=false
      })
    }
    // 在更改了二级分类的时候需要重新加载
    watch(()=>route.params.id,(newVal)=>{
      if(newVal && `/category/sub/${newVal}` === route.path){
        finished.value=false
        goodsList.value=[] //导致列表空的，加载更多组件顶上来，进入可视区
        reqParams = {
          page:1,
          pagesize:20,
        }
      }
    })
    //1. 更改排序组件的筛选数据，重新请求
    const  changeSort=(sortParams)=>{
      // console.log(sortParams);
      finished.value = false
      // 合并请求参数，保留之前参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodsList.value = []
    }
    //2. 更改排序组件的筛选数据，重新请求
    const changeFilter = (filterParams) => {
      finished.value = false
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []
    }
    return {loading,finished,getData,goodsList,changeSort,changeFilter}
  }
}
</script>


<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>